

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.nn.optim.rprop &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.nn.optim.rprop</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.nn.optim.rprop</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;rprop&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">ops</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">functional</span> <span class="k">as</span> <span class="n">F</span><span class="p">,</span> <span class="n">operations</span> <span class="k">as</span> <span class="n">P</span>
<span class="kn">import</span> <span class="nn">mindspore.common.dtype</span> <span class="k">as</span> <span class="nn">mstype</span>
<span class="kn">from</span> <span class="nn">mindspore.common.tensor</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="kn">from</span> <span class="nn">mindspore.common.parameter</span> <span class="kn">import</span> <span class="n">Parameter</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span> <span class="k">as</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Rel</span>
<span class="kn">from</span> <span class="nn">.optimizer</span> <span class="kn">import</span> <span class="n">Optimizer</span>
<span class="kn">from</span> <span class="nn">.optimizer</span> <span class="kn">import</span> <span class="n">opt_init_args_register</span>


<div class="viewcode-block" id="Rprop"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.Rprop.html#mindspore.nn.Rprop">[docs]</a><span class="k">class</span> <span class="nc">Rprop</span><span class="p">(</span><span class="n">Optimizer</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Implements Resilient backpropagation.</span>

<span class="sd">    Further information about this implementation can be found at  `A Direct Adaptive Method for Faster Backpropagation</span>
<span class="sd">    Learning: The RPROP Algorithm &lt;http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.21.1417&gt;`_.</span>

<span class="sd">    The updating formulas are as follows:</span>

<span class="sd">    .. math::</span>
<span class="sd">            \begin{gather*}</span>
<span class="sd">            &amp;\hspace{-10mm}  \textbf{if} \:   g_{t-1} g_t  &gt; 0                                     \\</span>
<span class="sd">            &amp;\hspace{25mm}  \Delta_t \leftarrow \mathrm{min}(\Delta_{t-1} \eta_{+}, \Delta_{max}) \\</span>
<span class="sd">            &amp;\hspace{0mm}  \textbf{else if}  \:  g_{t-1} g_t &lt; 0                                 \\</span>
<span class="sd">            &amp;\hspace{25mm}  \Delta_t \leftarrow \mathrm{max}(\Delta_{t-1} \eta_{-}, \Delta_{min}) \\</span>
<span class="sd">            &amp;\hspace{-25mm}  \textbf{else}  \:                                                      \\</span>
<span class="sd">            &amp;\hspace{-5mm}  \Delta_t \leftarrow \Delta_{t-1}                                      \\</span>
<span class="sd">            &amp;\hspace{15mm} w_{t} \leftarrow w_{t-1}- \Delta_{t} \mathrm{sign}(g_t)                \\</span>
<span class="sd">            \end{gather*}</span>

<span class="sd">    :math:`\Delta_{min/max}` represents the min/max step size, :math:`\eta_{+/-}` represents the factors of</span>
<span class="sd">    etaminus and etaplus, :math:`g` represents `gradients`, :math:`w` represents `parameters`.</span>

<span class="sd">    Note:</span>
<span class="sd">        If parameters are not grouped, the `weight_decay` in optimizer will be applied on the parameters without &#39;beta&#39;</span>
<span class="sd">        or &#39;gamma&#39; in their names. Users can group parameters to change the strategy of decaying weight. When parameters</span>
<span class="sd">        are grouped, each group can set `weight_decay`, if not, the `weight_decay` in optimizer will be applied.</span>

<span class="sd">    Args:</span>
<span class="sd">        params (Union[list[Parameter], list[dict]]): Must be list of `Parameter` or list of `dict`. When the</span>
<span class="sd">            `parameters` is a list of `dict`, the &quot;params&quot;, &quot;lr&quot;, &quot;weight_decay&quot;, &quot;grad_centralization&quot; and</span>
<span class="sd">            &quot;order_params&quot; are the keys can be parsed.</span>

<span class="sd">            - params: Required. Parameters in current group. The value must be a list of `Parameter`.</span>

<span class="sd">            - lr: Optional. If &quot;lr&quot; in the keys, the value of corresponding learning rate will be used.</span>
<span class="sd">              If not, the `learning_rate` in optimizer will be used. Fixed and dynamic learning rate are supported.</span>

<span class="sd">            - weight_decay: Optional. If &quot;weight_decay&quot; in the keys, the value of corresponding weight decay</span>
<span class="sd">              will be used. If not, the `weight_decay` in the optimizer will be used.</span>

<span class="sd">            - grad_centralization: Optional. Must be Boolean. If &quot;grad_centralization&quot; is in the keys, the set value</span>
<span class="sd">              will be used. If not, the `grad_centralization` is False by default. This configuration only works on the</span>
<span class="sd">              convolution layer.</span>

<span class="sd">            - order_params: Optional. When parameters is grouped, this usually is used to maintain the order of</span>
<span class="sd">              parameters that appeared in the network to improve performance. The value should be parameters whose</span>
<span class="sd">              order will be followed in optimizer.</span>
<span class="sd">              If `order_params` in the keys, other keys will be ignored and the element of &#39;order_params&#39; must be in</span>
<span class="sd">              one group of `params`.</span>

<span class="sd">        learning_rate (Union[float, int, Tensor, Iterable, LearningRateSchedule]):</span>

<span class="sd">            - float: The fixed learning rate value. Must be equal to or greater than 0.</span>

<span class="sd">            - int: The fixed learning rate value. Must be equal to or greater than 0. It will be converted to float.</span>

<span class="sd">            - Tensor: Its value should be a scalar or a 1-D vector. For scalar, fixed learning rate will be applied.</span>
<span class="sd">              For vector, learning rate is dynamic, then the i-th step will take the i-th value as the learning rate.</span>

<span class="sd">            - Iterable: Learning rate is dynamic. The i-th step will take the i-th value as the learning rate.</span>

<span class="sd">            - LearningRateSchedule: Learning rate is dynamic. During training, the optimizer calls the instance of</span>
<span class="sd">              LearningRateSchedule with step as the input to get the learning rate of current step.</span>

<span class="sd">        etas (tuple[float, float]): The factor of multiplicative increasing or</span>
<span class="sd">            descreasing(etaminus, etaplus).</span>
<span class="sd">        step_sizes(tuple[float, float]): The allowed minimal and maximal step size(min_step_sizes, max_step_size).</span>
<span class="sd">        weight_decay (int, float): Weight decay (L2 penalty). It must be equal to or greater than 0. Default: 0.0.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **gradients** (tuple[Tensor]) - The gradients of `params`, the shape is the same as `params`.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor[bool], the value is True.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `learning_rate` is not one of int, float, Tensor, Iterable, LearningRateSchedule.</span>
<span class="sd">        TypeError: If element of `parameters` is neither Parameter nor dict.</span>
<span class="sd">        TypeError: If `step_sizes` or `etas` is not a tuple.</span>
<span class="sd">        ValueError: If maximal step size is less than minimal step size.</span>
<span class="sd">        ValueError: If the length of `step_sizes` or `etas` is not equal to 2.</span>
<span class="sd">        TypeError: If  the element in `etas` or `step_sizes` is not a float.</span>
<span class="sd">        ValueError: If `etaminus` is not in the range of (0, 1) or `etaplus` is not greater than 1.</span>
<span class="sd">        TypeError: If `weight_decay` is neither float nor int.</span>
<span class="sd">        ValueError: If `weight_decay` is less than 0.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import nn, Model</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; net = Net()</span>
<span class="sd">        &gt;&gt;&gt; #1) All parameters use the same learning rate and weight decay</span>
<span class="sd">        &gt;&gt;&gt; optim = nn.Rprop(params=net.trainable_params())</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; #2) Use parameter groups and set different values</span>
<span class="sd">        &gt;&gt;&gt; conv_params = list(filter(lambda x: &#39;conv&#39; in x.name, net.trainable_params()))</span>
<span class="sd">        &gt;&gt;&gt; no_conv_params = list(filter(lambda x: &#39;conv&#39; not in x.name, net.trainable_params()))</span>
<span class="sd">        &gt;&gt;&gt; group_params = [{&#39;params&#39;: conv_params,&#39;grad_centralization&#39;:True},</span>
<span class="sd">        ...                 {&#39;params&#39;: no_conv_params, &#39;lr&#39;: 0.01},</span>
<span class="sd">        ...                 {&#39;order_params&#39;: net.trainable_params()}]</span>
<span class="sd">        &gt;&gt;&gt; optim = nn.Rprop(group_params, learning_rate=0.1, weight_decay=0.0)</span>
<span class="sd">        &gt;&gt;&gt; # The conv_params&#39;s parameters will use default learning rate of 0.1 default weight decay of 0.0 and grad</span>
<span class="sd">        &gt;&gt;&gt; # centralization of True.</span>
<span class="sd">        &gt;&gt;&gt; # The no_conv_params&#39;s parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad</span>
<span class="sd">        &gt;&gt;&gt; # centralization of False.</span>
<span class="sd">        &gt;&gt;&gt; # The final parameters order in which the optimizer will be followed is the value of &#39;order_params&#39;.</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; loss = nn.SoftmaxCrossEntropyWithLogits()</span>
<span class="sd">        &gt;&gt;&gt; model = Model(net, loss_fn=loss, optimizer=optim)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@opt_init_args_register</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">learning_rate</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">etas</span><span class="o">=</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">),</span> <span class="n">step_sizes</span><span class="o">=</span><span class="p">(</span><span class="mf">1e-6</span><span class="p">,</span> <span class="mf">50.</span><span class="p">),</span> <span class="n">weight_decay</span><span class="o">=</span><span class="mf">0.</span><span class="p">):</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">Rprop</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">learning_rate</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">weight_decay</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">etas</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;For Rprop, etas should be a tuple, but got </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">etas</span><span class="p">)))</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">etas</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;For Rprop, etas should be a tuple with the size of 2, but got </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">etas</span><span class="p">)))</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">step_sizes</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;For Rprop, step_sizes should be a tuple, but got </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">etas</span><span class="p">)))</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">step_sizes</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;For Rprop, step_sizes should be a tuple with the size of 2, &quot;</span>
                             <span class="s2">&quot;but got </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">step_sizes</span><span class="p">)))</span>

        <span class="k">if</span> <span class="n">step_sizes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">step_sizes</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;For Rprop, maximal step size should not be less than minimal step size, &quot;</span>
                             <span class="s2">&quot;but got </span><span class="si">{}</span><span class="s2"> &gt; </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">step_sizes</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">step_sizes</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>

        <span class="n">validator</span><span class="o">.</span><span class="n">check_float_range</span><span class="p">(</span><span class="n">etas</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">INC_NEITHER</span><span class="p">,</span> <span class="s2">&quot;etaminus&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;etaplus&quot;</span><span class="p">,</span> <span class="n">etas</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">etas</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mf">1.0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;For Rprop, etaplus should be greater than 1.0, but got etaplus </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">etas</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>

        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;min_step_sizes&quot;</span><span class="p">,</span> <span class="n">step_sizes</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;max_step_sizes&quot;</span><span class="p">,</span> <span class="n">step_sizes</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">etaminus</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">etaplus</span> <span class="o">=</span> <span class="n">etas</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">step_size_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">step_size_max</span> <span class="o">=</span> <span class="n">step_sizes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prev</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s2">&quot;prev&quot;</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="s1">&#39;zeros&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">step_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s2">&quot;step_size&quot;</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="s1">&#39;zeros&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">step</span> <span class="o">=</span> <span class="n">Parameter</span><span class="p">(</span><span class="n">Tensor</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;step&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">fill</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Fill</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sign</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Sign</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assign</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Assign</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assignadd</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">AssignAdd</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cast</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Cast</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">select</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Select</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ones_like</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">OnesLike</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gradients</span><span class="p">):</span>
        <span class="n">gradients</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">decay_weight</span><span class="p">(</span><span class="n">gradients</span><span class="p">)</span>
        <span class="n">gradients</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gradients_centralization</span><span class="p">(</span><span class="n">gradients</span><span class="p">)</span>
        <span class="n">gradients</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scale_grad</span><span class="p">(</span><span class="n">gradients</span><span class="p">)</span>
        <span class="n">lrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_lr</span><span class="p">()</span>
        <span class="n">success</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="p">(</span><span class="n">grad</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">prev</span><span class="p">,</span> <span class="n">step_size</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">gradients</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="p">,</span>
                                                                   <span class="bp">self</span><span class="o">.</span><span class="n">prev</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">step_size</span><span class="p">)):</span>
            <span class="n">lr</span> <span class="o">=</span> <span class="n">lrs</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_group_lr</span> <span class="k">else</span> <span class="n">lrs</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">step</span> <span class="o">==</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="n">step_size_fp32</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ones_like</span><span class="p">(</span><span class="n">step_size</span><span class="p">)</span> <span class="o">*</span> <span class="n">lr</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">step_size_fp32</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">step_size</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

            <span class="n">gradient_fp32</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">grad</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
            <span class="n">param_fp32</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">param</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

            <span class="n">sign</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">gradient_fp32</span> <span class="o">*</span> <span class="n">prev</span><span class="p">)</span>
            <span class="n">sign</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">sign</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">sign</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">etaplus</span><span class="p">),</span> <span class="n">sign</span><span class="p">)</span>
            <span class="n">sign</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">sign</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">sign</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">etaminus</span><span class="p">),</span> <span class="n">sign</span><span class="p">)</span>
            <span class="n">sign</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">sign</span> <span class="o">==</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">sign</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="mf">1.</span><span class="p">),</span> <span class="n">sign</span><span class="p">)</span>

            <span class="n">step_size_fp32</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">clip_by_value</span><span class="p">(</span><span class="n">step_size_fp32</span> <span class="o">*</span> <span class="n">sign</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">step_size_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">step_size_max</span><span class="p">)</span>

            <span class="n">gradient_update</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">sign</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">etaminus</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">sign</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="mf">0.</span><span class="p">),</span>
                                          <span class="n">gradient_fp32</span><span class="p">)</span>
            <span class="n">next_param</span> <span class="o">=</span> <span class="n">param_fp32</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">gradient_update</span><span class="p">)</span> <span class="o">*</span> <span class="n">step_size_fp32</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">param</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">next_param</span><span class="p">,</span> <span class="n">param</span><span class="o">.</span><span class="n">dtype</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">prev</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">gradient_update</span><span class="p">,</span> <span class="n">prev</span><span class="o">.</span><span class="n">dtype</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">step_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">step_size_fp32</span><span class="p">,</span> <span class="n">step_size</span><span class="o">.</span><span class="n">dtype</span><span class="p">))</span>

        <span class="n">success</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">depend</span><span class="p">(</span><span class="n">success</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">assignadd</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">step</span><span class="p">,</span> <span class="mf">1.</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">success</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>